cmake_minimum_required(VERSION 3.10)
project(spice VERSION 0.1 LANGUAGES CXX C)

SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
##
## project definition
##
#project('spice', 'c', 'cpp',
#        version : run_command('build-aux/git-version-gen', meson.source_root() + '/.tarball-version', check : true).stdout().strip(),
#        license : 'LGPLv2.1',
#        meson_version : '>= 0.49',
#        default_options : ['buildtype=debugoptimized',
#                           'warning_level=2'])
#
#meson.add_dist_script('build-aux/meson-dist', meson.project_version(), meson.source_root())

#message('Updating submodules')
#run_command('build-aux/meson/check-spice-common', check : true)

include(CheckFunctionExists)
#include(CTest)
enable_testing()

add_subdirectory(subprojects/spice-common)
set(spice_common_global_cflags ${spice_common_global_cflags_ps})

#
##
## soversion
## The versioning is defined by the forumla (CURRENT-AGE.AGE.REVISION)
##
## XXX: KEEP IN SYNC WITH configure.ac file
##
## Follow the libtool manual for the so version:
## http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
## - If the library source code has changed at all since the last update,
##   then increment revision (‘c:r:a’ becomes ‘c:r+1:a’).
## - If any interfaces have been added, removed, or changed since the last update,
##   increment current, and set revision to 0.
## - If any interfaces have been added since the last public release,
##   then increment age.
## - If any interfaces have been removed or changed since the last public release,
##   then set age to 0.
##
##
#spice_server_current = 15
set(spice_server_current 15)
#spice_server_revision = 3
set(spice_server_revision 3)
#spice_server_age = 14
set(spice_server_age 14)
#spice_server_so_version = '@0@.@1@.@2@'.format(spice_server_current - spice_server_age,
#                                               spice_server_age,
#                                               spice_server_revision)
math(EXPR SO_VERSION_M "${spice_server_current} - ${spice_server_age}")
set(spice_server_so_version "${SO_VERSION_M}.${spice_server_age}.${spice_server_revision}")
message("libspice.so version: " ${spice_server_so_version})
#
## some global vars
#spice_server_global_cflags = ['-DSPICE_SERVER_INTERNAL',
#                              '-DG_LOG_DOMAIN="Spice"',
#                              '-Wno-sign-compare',
#                              '-Wno-unused-parameter']
#

#set(spice_server_global_cflags "-DSPICE_SERVER_INTERNAL
#                                -DG_LOG_DOMAIN=\"Spice\"
#                                -Wno-sign-compare
#                                -Wno-unused-parameter")
set(spice_server_global_cflags
    "-DSPICE_SERVER_INTERNAL"
#    "-DG_LOG_DOMAIN=Spice"
    "-Wno-sign-compare"
    "-Wno-unused-parameter"
    "${spice_common_global_cflags}"
    )

#compiler = meson.get_compiler('c')
#cxx_compiler = meson.get_compiler('cpp')
#spice_server_config_data = configuration_data()
#spice_server_include = [include_directories('.')]
#spice_server_deps = [dependency('threads')]
#spice_server_link_args = []
#spice_server_requires = ''
#spice_protocol_version='0.14.3'

set(spice_server_include ${CMAKE_CURRENT_SOURCE_DIR})
set(spice_server_include "${spice_server_include};${CMAKE_CURRENT_BINARY_DIR}")
message("addeb current binarya : " ${spice_server_include} )

set(spice_protocol_version "0.14.3")

#
##
## Spice common subproject
##
#spice_common = subproject('spice-common',
#                          default_options : [
#                            'generate-code=server',
#                            'spice-protocol-version=@0@'.format(spice_protocol_version),
#                          ])
#spice_server_config_data.merge_from(spice_common.get_variable('spice_common_config_data'))
#spice_server_deps += spice_common.get_variable('spice_common_server_dep')
#spice_protocol_version = spice_common.get_variable('spice_protocol_version')


#
##
## check for system headers
##
#headers = ['sys/time.h',
#           'execinfo.h',
#           'linux/sockios.h',
#           'pthread_np.h']
#
#foreach header : headers
#  if compiler.has_header(header)
#    spice_server_config_data.set('HAVE_@0@'.format(header.underscorify().to_upper()), '1')
#  endif
#endforeach
check_include_files(sys/time.h have_sys_time_h)
if (have_sys_time_h)
    set(HAVE_SYS_TIME_H 1) #for config.h
endif()
check_include_files(execinfo.h have_execinfo_h)
if (have_execinfo_h)
    set(HAVE_EXECINFO_H 1) #for config.h
endif()
check_include_files(linux/sockios.h have_linux_sockios_h)
if (have_linux_sockios_h)
    set(HAVE_LINUX_SOCKIOS_H 1) #for config.h
endif()
check_include_files(pthread_np.h have_pthread_np_h)
if (have_pthread_np_h)
    set(HAVE_PTHRAD_NP_H 1) #for config.h
endif()

#
## TCP_KEEPIDLE definition in netinet/tcp.h
#if compiler.has_header_symbol('netinet/tcp.h', 'TCP_KEEPIDLE')
#  spice_server_config_data.set('HAVE_TCP_KEEPIDLE', '1')
#endif
#
include(CheckSymbolExists)
check_symbol_exists(TCP_KEEPIDLE netinet/tcp.h have_tcp_keepidle)
if (have_tcp_keepidle)
    set(HAVE_TCP_KEEPIDLE 1) #config.h
endif()

##
## check for mandatory dependencies
##
#glib_version = '2.38'
#glib_version_info = '>= @0@'.format(glib_version)
#pixman_version = '>= 0.17.7'

set(glib_version ${glib_version_ps_spice_common})

#
#deps = {'glib-2.0'       : glib_version_info,
#        'pixman-1'       : pixman_version,
#        'openssl'        : '>= 1.0.0'}
#
#foreach dep, version : deps
#  spice_server_deps += dependency(dep, version : version)
#endforeach
#
## TODO: specify minimum version for jpeg and zlib?
#foreach dep : ['libjpeg', 'zlib']
#  spice_server_deps += dependency(dep)
#endforeach
find_package(MyJpegTurbo REQUIRED)
list(APPEND spice_server_deps ${JPEG_TURBO_LIBRARY})
set(spice_server_include "${spice_server_include};${JPEG_INCLUDE_DIR}")
find_package(MyZlib REQUIRED)
list(APPEND spice_server_deps ${ZLIB_LIBRARY})
set(spice_server_include "${spice_server_include};${ZLIB_INCLUDE_DIR}")

#
#if host_machine.system() != 'windows'
#  foreach dep : ['rt', 'm']
#    spice_server_deps += compiler.find_library(dep)
#  endforeach
#else
#  foreach dep : ['ws2_32', 'shlwapi']
#    spice_server_deps += compiler.find_library(dep)
#  endforeach
#endif
#
##
## Non-mandatory/optional dependencies
##
#optional_deps = {'opus' : '>= 0.9.14'}
#foreach dep, version : optional_deps
#  d = dependency(dep, required : get_option(dep), version : version)
#  if d.found()
#    spice_server_deps += d
#    spice_server_config_data.set('HAVE_@0@'.format(dep.underscorify().to_upper()), '1')
#  endif
#endforeach
#
## gstreamer
#spice_server_has_gstreamer = false
#spice_server_gst_version = get_option('gstreamer')
#if spice_server_gst_version != 'no'
#  gst_deps = ['gstreamer', 'gstreamer-base', 'gstreamer-app', 'gstreamer-video']
#  foreach dep : gst_deps
#    dep = '@0@-@1@'.format(dep, spice_server_gst_version)
#    spice_server_deps += dependency(dep)
#  endforeach
#  spice_server_deps += dependency('orc-0.4')
#
#  gst_def = 'HAVE_GSTREAMER_1_0'
#  spice_server_config_data.set(gst_def, '1')
#  spice_server_has_gstreamer = true
#endif
set(spice_server_has_gstreamer FALSE)
set(gstreamer "1.0" CACHE STRING "Enable gstreamer support") #combo: "1.0" / "no"
set(spice_server_gst_version ${gstreamer})
if (NOT ${spice_server_gst_version} VERSION_EQUAL "no")

    find_package(MyGstreamer REQUIRED)
    list(APPEND spice_server_deps ${GSTREAMER_LIBRARY})
    set(spice_server_include "${spice_server_include};${GSTREAMER_INCLUDE_DIRS}")

    message("haha lala bobbi:!!!!!!!!!!!!!GSTREAMER_INCLUDE_DIRS is " ${GSTREAMER_INCLUDE_DIRS})
    find_package(MyGstreamerBase REQUIRED)
    list(APPEND spice_server_deps ${GSTREAMER_BASE_LIBRARY})

    find_package(MyGstreamerApp REQUIRED)
    list(APPEND spice_server_deps ${GSTREAMER_APP_LIBRARY})

    find_package(MyGstreamerVideo REQUIRED)
    list(APPEND spice_server_deps ${GSTREAMER_VIDEO_LIBRARY})

    find_package(MyOrc REQUIRED)
    list(APPEND spice_server_deps ${ORC_LIBRARY})
    set(spice_server_include "${spice_server_include};${ORC_INCLUDE_DIRS}")

    set(spice_server_has_gstreamer TRUE)
    set(HAVE_GSTREAMER_1_0 1) #config.h
endif()


#
## lz4
#spice_server_has_lz4 = false
#if get_option('lz4')
#  lz4_dep = dependency('liblz4', required : false, version : '>= 129')
#  if not lz4_dep.found()
#    lz4_dep = dependency('liblz4', version : '>= 1.7.3')
#  endif
#
#  if compiler.has_function('LZ4_compress_fast_continue', dependencies : lz4_dep)
#    spice_server_config_data.set('HAVE_LZ4_COMPRESS_FAST_CONTINUE', '1')
#  endif
#
#  spice_server_deps += lz4_dep
#  spice_server_config_data.set('USE_LZ4', '1')
#  spice_server_has_lz4 = true
#endif
set(spice_server_has_lz4 FALSE)
option(lz4 "Enable lz4 compression support" TRUE)
if (lz4)
    find_package(MyLz4 REQUIRED)
    message("bybobbi: LZ4_VERSION is " ${LZ4_VERSION})
    if (NOT ${LZ4_VERSION} VERSION_GREATER_EQUAL "129")
        if (NOT ${LZ4_VERSION} VERSION_GREATER_EQUAL "1.7.3")
            message(FATAL "lz4 version not support!")
        endif()
    endif()

    check_function_exists(LZ4_compress_fast_continue HAVE_LZ4_COMPRESS_FAST_CONTINUE)
    if (HAVE_LZ4_COMPRESS_FAST_CONTINUE)
        set(HAVE_LZ4_COMPRESS_FAST_CONTINUE 1) #for config.h
    endif()

    list(APPEND spice_server_deps ${LZ4_LIBRARY})
    set(spice_server_include "${spice_server_include};${LZ4_INCLUDE_DIR}")

    set(USE_LZ4 1) #config.h
    set(spice_server_has_lz4 TRUE)

endif()

#
## sasl
#spice_server_has_sasl = false
#if get_option('sasl')
#  spice_server_deps += dependency('libsasl2')
#  spice_server_config_data.set('HAVE_SASL', '1')
#  spice_server_has_sasl = true
#endif
#

set(spice_server_has_sasl FALSE)
option(sasl "Use cyrus SASL authentication" TRUE)
if (sasl)
    find_package(MySasl REQUIRED)
    if (MySasl_FOUND)
        set(HAVE_SASL 1) #config.h
        list(APPEND spice_server_deps ${SASL_LIBRARY})
        set(spice_server_include "${spice_server_include};${SASL_INCLUDE_DIR}")
        set(spice_server_has_sasl TRUE)
    endif()
endif()

## smartcard check
#spice_server_has_smartcard = false
#smartcard_dep = dependency('libcacard', required : get_option('smartcard'), version : '>= 2.5.1')
#if smartcard_dep.found()
#  spice_server_deps += smartcard_dep
#  spice_server_config_data.set('USE_SMARTCARD', '1')
#  spice_server_has_smartcard = true
#  spice_server_requires += 'libcacard >= 2.5.1 '
#endif
#
set(spice_server_has_smartcard FALSE)
option(smartcard "Use cyrus SASL authentication" TRUE)
if (smartcard)
        find_package(MyCacard REQUIRED)
        #message("CACARD_VERSION is " ${CACARD_VERSION})
        if (${CACARD_VERSION} STRGREATER_EQUAL "2.5.1")
            list(APPEND spice_server_deps ${CACARD_LIBRARY})
            set(spice_server_include "${spice_server_include};${CACARD_INCLUDE_DIR}")
            set(spice_server_has_smartcard TRUE)
        endif()
endif()

## smartcard check
#spice_server_has_smartcard = false
#smartcard_dep = dependency('libcacard', required : get_option('smartcard'), version : '>= 2.5.1')
#if smartcard_dep.found()
#  spice_server_deps += smartcard_dep
#  spice_server_config_data.set('USE_SMARTCARD', '1')
#  spice_server_has_smartcard = true
#  spice_server_requires += 'libcacard >= 2.5.1 '
#endif
#

##
## global C defines
##
#glib_encoded_version = 'GLIB_VERSION_@0@'.format(glib_version.underscorify())
#spice_server_global_cflags += ['-DGLIB_VERSION_MIN_REQUIRED=@0@'.format(glib_encoded_version),
#                               '-DGLIB_VERSION_MAX_ALLOWED=@0@'.format(glib_encoded_version)]
#
#add_project_arguments(compiler.get_supported_arguments(spice_server_global_cflags),
#                      language : 'c')
#

add_definitions(${spice_server_global_cflags})
message("bobbi: spice_server_global_cflags is " ${spice_server_global_cflags})
#set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${spice_server_global_cflags})

#spice_server_global_cxxflags = spice_server_global_cflags
#spice_server_global_cxxflags += [
#  '-fno-exceptions',
#  '-Wno-suggest-final-methods',
#  '-Wno-suggest-final-types',
#  '-Wno-array-bounds',
#  '-Wno-stringop-overflow',
#  '-Wno-narrowing',
#  '-Wno-missing-field-initializers',
#  '-Wno-deprecated-declarations',
#  '-Wshadow',
#]
set(spice_server_global_cxxflags ${spice_server_global_cflags})
list(APPEND spice_server_global_cxxflags
    "-fno-exceptions"
    "-Wno-suggest-final-methods"
    "-Wno-suggest-final-types"
    "-Wno-array-bounds"
    "-Wno-stringop-overflow"
    "-Wno-narrowing"
    "-Wno-missing-field-initializers"
    "-Wno-deprecated-declarations"
    "-Wshadow"
    )
#add_project_arguments(cxx_compiler.get_supported_arguments(spice_server_global_cxxflags),
#                      language : 'cpp')
#
#set(CMAKE_CXX_FLAGS ${spice_server_global_cxxflags})
add_definitions(${spice_server_global_cxxflags})
##
## Subdirectories
##
#subdir('server')
#subdir('tools')
#subdir('docs')
add_subdirectory(server)
#add_subdirectory(tools)
#add_subdirectory(docs)
#
##
## write config.h
##
#spice_server_config_data.set_quoted('VERSION', meson.project_version())
#spice_server_config_data.set('SPICE_USE_SAFER_CONTAINEROF', '1')

set(VERSION 0.1) #config.h
set(SPICE_USE_SAFER_CONTAINEROF 1) #config.h
#
#if get_option('statistics')
#  spice_server_config_data.set('RED_STATISTICS', '1')
#endif

option(statistics "Build SPICE with statistic code enabled" FALSE)
if (statistics)
        set(RED_STATISTICS 1) #config.h
endif()

#
## Minimal Win32 version
#if host_machine.system() == 'windows'
#  spice_server_config_data.set('_WIN32_WINNT', '0x600')
#endif

if (WIN32)
    set(_WIN32_WINNT, 0x600) #config.h
endif(WIN32)
#
#configure_file(output : 'config.h',
#               configuration : spice_server_config_data)
#
#run_target('doxy', command : './doxygen.sh')

configure_file(config.h.cmake config.h)
